package com.fxp.project.jiuselu.web.models.sample.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fxp.project.jiuselu.commons.constant.ConstantCode;
import com.fxp.project.jiuselu.commons.exceptions.ParamException;
import com.fxp.project.jiuselu.commons.utils.RandomUtils;
import com.fxp.project.jiuselu.web.business.v1.request.order.ReqOrderCart;
import com.fxp.project.jiuselu.web.models.goods.mapper.JslGoodsAttrMapper;
import com.fxp.project.jiuselu.web.models.goods.mapper.JslGoodsMapper;
import com.fxp.project.jiuselu.web.models.goods.vo.GoodsAttrVo;
import com.fxp.project.jiuselu.web.models.goods.vo.GoodsVo;
import com.fxp.project.jiuselu.web.models.order.entity.JslCart;
import com.fxp.project.jiuselu.web.models.order.entity.JslOrderGoods;
import com.fxp.project.jiuselu.web.models.order.mapper.JslCartMapper;
import com.fxp.project.jiuselu.web.models.sample.entity.SampleOrder;
import com.fxp.project.jiuselu.web.models.sample.entity.SapmleGoods;
import com.fxp.project.jiuselu.web.models.sample.entity.vo.SampleOrderVo;
import com.fxp.project.jiuselu.web.models.sample.mapper.SampleOrderMapper;
import com.fxp.project.jiuselu.web.models.sample.mapper.SapmleGoodsMapper;
import com.fxp.project.jiuselu.web.models.sample.service.ISampleOrderService;
import com.fxp.project.jiuselu.web.models.user.entity.JslUser;
import com.fxp.project.jiuselu.web.models.user.entity.JslUserAddress;
import com.fxp.project.jiuselu.web.models.user.mapper.JslUserAddressMapper;
import com.fxp.project.jiuselu.web.models.user.mapper.JslUserMapper;
import com.fxp.project.jiuselu.web.models.user.service.IJslUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 样品订单Service业务层处理
 *
 * @author Jason.Wang
 * @date 2022-06-08
 */
@Service
public class SampleOrderServiceImpl implements ISampleOrderService
{
    @Autowired
    private SampleOrderMapper sampleOrderMapper;

    @Autowired
    JslUserAddressMapper jslUserAddressMapper;

    @Autowired
    SapmleGoodsMapper sapmleGoodsMapper;

    @Autowired
    JslCartMapper jslCartMapper;

    @Autowired
    JslGoodsMapper jslGoodsMapper;

    @Autowired
    JslGoodsAttrMapper jslGoodsAttrMapper;

    @Autowired
    IJslUserService userService;


    @Autowired
    JslUserMapper jslUserMapper;
    /**
     * 查询样品订单
     *
     * @param sampleId 样品订单主键
     * @return 样品订单
     */
    @Override
    public SampleOrderVo selectSampleOrderBySampleId(String sampleId)
    {
        return sampleOrderMapper.selectSampleOrderBySampleId(sampleId);
    }

    /**
     * 查询样品订单列表
     *
     * @param sampleOrder 样品订单
     * @return 样品订单
     */
    @Override
    public PageInfo<?> selectSampleOrderList(SampleOrder sampleOrder)
    {
        List<SampleOrder> sampleOrders = sampleOrderMapper.selectSampleOrderList(sampleOrder);
        return new PageInfo<>(sampleOrders);
    }

    @Override
    public List<SampleOrderVo> selectSampleListByUserIdStatus(Long userId, Integer orderTotalStatus) {
        return sampleOrderMapper.selectSampleListByUserIdStatus(userId,orderTotalStatus);
    }

    /**
     * 新增样品订单
     *
     * @return 结果
     */
    @Override
    public Object insertSampleOrder(ReqOrderCart reqOrderCart,Long userId)
    {
        SampleOrder sampleOrder = new SampleOrder();
        // 查询当前用户提交的地址信息
        JslUserAddress jslUserAddress =
                jslUserAddressMapper.selectJslUserAddressByUserIdWithAddressId(userId, reqOrderCart.getAddressId());
        if (null == jslUserAddress) {
            throw new ParamException(ConstantCode.PARAM_EXCEPTION.getCode(), "当前选择的地址不存在，请重新提交");
        }
        QueryWrapper<JslUser> wrapper = new QueryWrapper<JslUser>().eq("user_id", userId).eq("level",0);
        JslUser jslUser = jslUserMapper.selectOne(wrapper);
        sampleOrder.setSampleId(String.format("YP%s", RandomUtils.getOrderNo()));
        sampleOrder.setConsignee(jslUserAddress.getName());
        sampleOrder.setCountry(0L);
        sampleOrder.setUserId(userId);
        sampleOrder.setUserName(jslUser.getNickName());
        sampleOrder.setProvinceId(jslUserAddress.getProvinceId());
        sampleOrder.setAddTime(LocalDateTime.now());
        sampleOrder.setProvinceName(jslUserAddress.getProvinceName());
        sampleOrder.setCityId(jslUserAddress.getCityId());
        sampleOrder.setCityName(jslUserAddress.getCityName());
        sampleOrder.setDistrictId(jslUserAddress.getDistrictId());
        sampleOrder.setDistrictName(jslUserAddress.getDistrictName());
        sampleOrder.setAddress(jslUserAddress.getAddress());
        sampleOrder.setPhone(jslUserAddress.getPhone());
        sampleOrderMapper.insertSampleOrder(sampleOrder);

        List<JslCart> cartList = jslCartMapper.selectBatchIds(reqOrderCart.getCartIds());
        if (CollectionUtils.isEmpty(cartList)) {
            throw new ParamException(ConstantCode.PARAM_EXCEPTION.getCode(), "当前没有商品提交，请到购物车重试");

        } else {
            for (JslCart cart : cartList) {
                GoodsVo goodsVo = jslGoodsMapper.selectGoodsVoByGoodsId(cart.getGoodsId());
                if (null == goodsVo || !goodsVo.getGoodsStatus().equals(1)) {
                    throw new ParamException(ConstantCode.PARAM_EXCEPTION.getCode(), "当前提交订单商品不存在或已下架");
                }
                // 查询当前商品规格信息
                GoodsAttrVo goodsAttrVo = jslGoodsAttrMapper.selectGoodsAttrVoByGoodsAttrId(cart.getAttrId());
                if (null == goodsAttrVo) {
                    throw new ParamException(ConstantCode.PARAM_EXCEPTION.getCode(), "当前提交订单商品不存在或已下架");
                }

                SapmleGoods sapmleGoods = new SapmleGoods();
                // 商品Id
                sapmleGoods.setGoodsId(goodsVo.getGoodsId());
                // 商品名称
                sapmleGoods.setGoodsName(goodsVo.getGoodsName());
                // 商品规格Id
                sapmleGoods.setGoodsAttrId(goodsAttrVo.getAttrId());
                // 商品规格名称
                sapmleGoods.setGoodsAttrName(goodsAttrVo.getAttrName());
                sapmleGoods.setGoodsAttrImage(goodsVo.getGoodsImages());
                sapmleGoods.setSampleId(sampleOrder.getSampleId());
                sapmleGoodsMapper.insertSapmleGoods(sapmleGoods);
            }
            //删除购物车信息
            jslCartMapper.deleteBatchIds(cartList.stream().map(JslCart::getId).collect(Collectors.toList()));
        }
        return sampleOrder.getSampleId();
    }
    /**
     * 修改样品订单
     *
     * @param sampleOrder 样品订单
     * @return 结果
     */
    @Override
    public int updateSampleOrder(SampleOrder sampleOrder)
    {
        return sampleOrderMapper.updateSampleOrder(sampleOrder);
    }

    /**
     * 批量删除样品订单
     *
     * @param sampleIds 需要删除的样品订单主键
     * @return 结果
     */
    @Override
    public int deleteSampleOrderBySampleIds(String[] sampleIds)
    {
        return sampleOrderMapper.deleteSampleOrderBySampleIds(sampleIds);
    }

    /**
     * 删除样品订单信息
     *
     * @param sampleId 样品订单主键
     * @return 结果
     */
    @Override
    public int deleteSampleOrderBySampleId(String sampleId)
    {
        return sampleOrderMapper.deleteSampleOrderBySampleId(sampleId);
    }
}
